<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-04 11:42:35
 * @Last Modified time: 2023-02-04 14:04:03
 */
namespace app\common\services\store;

use app\common\dao\store\GoodsCollectionDao;
use app\common\services\store\GoodsService;

use frank\base\BaseService;
use frank\exception\CommonException;
use frank\utils\helper\ArrayHelper;
use frank\services\CacheService;

/**
 * Class GoodsCollectionService
 */
class GoodsCollectionService extends BaseService
{

	public $action = 1;

    /**
     * GoodsCollectionService constructor.
     * @param GoodsCollectionDao $dao
     */
    public function __construct(GoodsCollectionDao $dao)
    {
        $this->dao = $dao;
    }

    public function set($goodsId,$uid){
    	$filter = ['goods_id'=>$goodsId,'user_id'=>$uid];
    	if ($this->dao->status($goodsId,$uid)) {
    		$this->action = 0;
            return $this->dao->delete($filter);
        }
        return $this->dao->create($filter);
    }

    /**
     * 获取列表
     */
    public function getGoodsCollectionList($uid)
    {
        [$page, $limit] = $this->getPageValue();
        $where = [];
        $where['user_id'] = $uid;
        $list = $this->dao->getGoodsCollectionList($where, $page, $limit,[]);
        $goodsService = GoodsService::instance();
        foreach ($list as $key => $item) {
        	$item['goods'] = $goodsService->getCacheDetail($item['goods_id']);
        }
        return $list;
    }

}